package com.cyj.dream.core.exception;

import com.cyj.dream.core.constant.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Description: 自定义异常实体类--强制同步
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.core.exception
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-26 10:58
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Data
@ApiModel(value = "BizException 自定义异常实体类", description = "异常实体类")
public class BizException extends RuntimeException {

    /**
     * 错误状态码
     */
    private Integer code;

    /**
     * 错误信息
     */
    @ApiModelProperty(value = "错误信息")
    private String message;

    public BizException() {
        super();
    }

    /**
     * 可以直接传入封装好的异常信息
     *
     * @param resultCode
     */
    public BizException(ResultCode resultCode) {
        super(resultCode.message());
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public BizException(ResultCode resultCode, Throwable cause) {
        super(resultCode.message(), cause);
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    /**
     * 直接传入错误信息返回 -1也可以
     *
     * @param message
     */
    public BizException(String message) {
        super(message);
        this.setCode(-1);
        this.message = message;
    }

    /**
     * 传入 code和message
     *
     * @param code
     * @param message
     */
    public BizException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BizException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }

    /**
     * 同步
     *
     * @return
     */
    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }

}
